
resource "null_resource" "provisioner" {
  count = signum(length(var.playbook)) == 1 ? 1 : 0

  triggers = {
    default = "${uuid()}"
    command   = "ansible-playbook ${var.dry_run ? "--check --diff" : ""} ${join(" ", compact(var.arguments))} ${length(compact(var.envs)) > 0 ? "-e" : ""} ${join(" -e ", compact(var.envs))} ${var.playbook}"
  }

  # provisioner "remote-exec" {
  #   inline = ["echo Connection Remote Host Success!"]

  #   # connection {
  #   #   type        = "ssh"
  #   #   user        = var.host
  #   #   private_key = file(var.ssh_key_private)
  #   # }
  # }

  provisioner "local-exec" {
    command = "ansible-playbook ${var.dry_run ? "--check --diff" : ""} ${join(" ", compact(var.arguments))} ${length(compact(var.envs)) > 0 ? "-e" : ""} ${join(" -e ", compact(var.envs))} ${var.playbook}"
  }

  lifecycle {
    create_before_destroy = true
  }
}
